package com.party.web.biz.asynexport;

import com.alibaba.fastjson.JSONObject;
import com.google.common.util.concurrent.*;
import com.party.common.utils.FileUtils;
import com.party.web.utils.excel.ExportExcel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

/**
 * Created by wei.li
 *
 * @date 2017/7/18 0018
 * @time 14:40
 */

@Service
public class AsynExportBizService {

    private static Logger logger = LoggerFactory.getLogger(AsynExportBizService.class);
    ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(500));

    /**
     * 导出excel
     *
     * @param title             题目
     * @param clazz             类
     * @param list              数据集合
     * @param realPath          导出路径
     * @param asynExportService 异步接口
     */
    public void export(String title, Class<?> clazz, List<?> list, String realPath, IAsynExportService asynExportService, String fileName, boolean isEnd) {
        ListenableFuture future = service.submit((Callable) () -> {
            //导出
            FileUtils.createDirectory(realPath);
            try {
                new ExportExcel(title, clazz).setDataList(list).writeFile(realPath + title).dispose();
            } catch (Exception e) {
                logger.error("{} 导出异常: 结果{}", clazz.getName(), JSONObject.toJSONString(list), e);
            }
            return true;
        });

        //回调
        Futures.addCallback(future, new FutureCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean b) {
                asynExportService.callBack(b, fileName, isEnd);
            }

            @Override
            public void onFailure(Throwable throwable) {
                logger.error("异步导出异常——" + title + ";" + realPath, throwable);
            }
        });
    }

    /**
     * 导出excel
     *
     * @param title             题目
     * @param clazz             类
     * @param list              数据集合
     * @param realPath          导出路径
     * @param asynExportService 异步接口
     */
    public void export2(String title, Class<?> clazz, List<?> list, String realPath, IAsynExportService asynExportService,
                        String fileName, boolean isEnd) throws Exception {
        try {
            //导出
            FileUtils.createDirectory(realPath);
            new ExportExcel(title, clazz).setDataList(list).writeFile(realPath + title).dispose();
            asynExportService.callBack(true, fileName, isEnd);
        } catch (Exception e) {
            logger.error("异步导出异常——" + title + ";" + realPath, e);
            throw e;
        }
    }

    /**
     * 导出excel
     *
     * @param title             题目
     * @param clazz             类
     * @param list              数据集合
     * @param realPath          导出路径
     * @param asynExportService 异步接口
     */
    public void export(String title, Class<?> clazz, List<?> list, String realPath, IAsynExportService asynExportService,
                       String fileName, boolean isEnd, String cloudName) throws Exception {
        try {
            //导出
            FileUtils.createDirectory(realPath);
            new ExportExcel(title, clazz).setDataList(list).writeFile(realPath + title).dispose();
            asynExportService.callBack(true, fileName, isEnd, cloudName);
        } catch (Exception e) {
            logger.error("异步导出异常——" + title + ";" + realPath, e);
            throw e;
        }
    }

    public String getBaseDir() {
        return System.getProperty("user.dir").replaceAll("\\\\", "/");
    }
}
